const Controller = require("egg").Controller;

class VideoController extends Controller {
  async getVideoList() {
    try {
      let currentPage = this.ctx.query.currentPage || 1;
      let pageSize = this.ctx.query.pageSize || 5;
      const data = await this.ctx.service.video.getVideoList({
        currentPage,
        pageSize,
      });
      this.ctx.body = {
        code: 200,
        data: data,
      };
    } catch (error) {
      this.ctx.body = {
        code: -1,
        data: null,
        message: error.message,
      };
    }
  }

  async findVideo() {
    const { ctx } = this;
    try {
      const { id } = ctx.request.body;
      const video = await this.ctx.service.video.findVideoById(id);
      this.ctx.body = {
        code: 200,
        data: video,
      };
    } catch (error) {
      this.ctx.body = {
        code: -1,
        data: null,
        message: error.message,
      };
    }
  }

  async saveVideo() {
    const { ctx } = this;

    try {
      const { id, title, content, thumbnail, video_url } = ctx.request.body;
      const { id: user_id } = ctx.decode;
      const result = await this.ctx.service.video.saveVideo({
        id,
        title,
        content,
        thumbnail,
        video_url,
        user_id,
      });

      this.ctx.body = {
        code: 200,
        data: result,
      };
    } catch (error) {
      this.ctx.body = {
        code: -1,
        data: null,
        message: error.message,
      };
    }
  }

  //修改视频
  async modifyVideo() {
    const { ctx } = this;
    try {
      const { id, title, content, thumbnail, video_url } = ctx.request.body;
      const { id: user_id } = ctx.decode;
      const result = await this.ctx.service.video.modifyVideo({
        id,
        title,
        content,
        thumbnail,
        video_url,
        user_id,
      });
      this.ctx.body = {
        code: 200,
        data: result,
      };
    } catch (error) {
      this.ctx.body = {
        code: -1,
        data: null,
        message: error.message,
      };
    }
  }

  // 删除视频
  async deleteVideo() {
    const { ctx } = this;
    try {
      const { id } = ctx.request.body;
      const video = await this.ctx.service.video.deleteVideo(id);
      this.ctx.body = {
        code: 200,
        data: video,
      };
    } catch (error) {
      this.ctx.body = {
        code: -1,
        data: null,
        message: error.message,
      };
    }
  }
}

module.exports = VideoController;
